package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.MyOrder;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/myOrders")
public class MyOrderController {

    @Autowired
    private IMyOrderService myOrderService;
    @Autowired
    private IProductService productService;


    //查询已付款的订单
    @GetMapping("paids")
    public PageInfo getOrderByPaid (QueryObject qo,Long userId){
      return   myOrderService.getOrderByPaid(qo,userId);
    }

    //查询待付款的订单
    @GetMapping("obligation")
    public PageInfo getObligationOrder (QueryObject qo,Long userId){
      return   myOrderService.getObligationOrder(qo,userId);
    }

    //点击取消订单删除订单
    @DeleteMapping("{orderId}")
    public void deleteOrder(@PathVariable Long orderId){
        myOrderService.deleteOrderById(orderId);
    }

    //查询总共几条订单
    @GetMapping("{userId}")
    public Integer getTotalCount(@PathVariable Long userId){
        return myOrderService.getTotalCount(userId);
    }
    //查询待付款的订单数
    @GetMapping("{userId}/obligation")
    public Integer getCounts(@PathVariable Long userId){
        return myOrderService.getCounts(userId);
    }

    //根据id查询商品信息
    @GetMapping("{orderId}/products")
    public MyOrder getProductInfo(@PathVariable Long orderId){
        return myOrderService.getProductInfo(orderId);
    }

    //支付成功后修改支付状态
    @PutMapping
    public void updateState(Long orderId,Long number,Long productId){
        myOrderService.updateState(orderId);
        Long storeNum = productService.getStoreNum(productId);
        storeNum -= number;
        productService.updateStoreNum(productId,storeNum);
    }

    @PostMapping
    public Object save(MyOrder myOrder){
        myOrderService.save(myOrder);
        return myOrder.getId();
    }


}
